package conexaobd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class Conectabdoracle {
    public static Connection conn;
    private static PreparedStatement pst;
    private static ResultSet rs;
    private String url;
    private Properties props = new Properties();

    public void abreConexao(){
        try{
            url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
            Class.forName("oracle.jdbc.driver.OracleDriver");
            props.put("user", "ely");
            props.put("password", "123");
            conn = DriverManager.getConnection(url, props);
            System.out.println("Conexão realizada com sucesso!");
        } catch(Exception e){
            System.out.println("Erro: " + e.getMessage());
        }
    }
   
    public static void executaSQL(String sql){
        try{
          
            pst = conn.prepareStatement(sql);
            pst.executeUpdate();
        }catch (Exception erro){
            System.out.println("Erro: " + erro.getMessage());
        }
    }
    
    public static ResultSet executaConsulta(String sql){
        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            return rs;
            
        }catch(Exception erro){
            System.out.println("Erro: " + erro.getMessage());
            return null;
        }
    }
    
    public static PreparedStatement prepare(String sql){
        try{
            return conn.prepareStatement(sql);
        }catch(Exception erro){
            System.out.println("Erro: " + erro.getMessage());
            return null;
        }
    }
    public static void fechaConexao(){
        try {
            if(rs!=null)
                rs.close();
            if(pst!=null)
                pst.close();
            
            if(conn!=null)
                conn.close();
        } catch (Exception erro) {
            System.out.println("Erro " + erro.getMessage());
        }
        
    }
}